<!DOCTYPE hmtl>
<html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <link href="styles/styles.css" type="text/css" rel="stylesheet"/>
       <script src="scripts/scripts.js" type="text/javascript"></script>
       <title>Register</title>
       <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
       <script type="text/javascript">
 
         $(document).ready(function(){
            $("#username").change(function(){
                 $("#message").html("<img src='img/ajax_loader.gif'/> checking...");       
 
            var username=$("#username").val();
 
              $.ajax({
                    type:"post",
                    url:"check.php",
                    data:"username="+username,
                        success:function(data){
                        if(data==0){
                            $("#message").html("<img src='img/tick.png' /> Username available");
                        }
                        else{
                            $("#message").html("<img src='img/cross.png' /> Username already taken");
                        }
                    }
                 });
 
            });
 
         });
 
       </script>
       <link href='http://fonts.googleapis.com/css?family=Dosis' rel='stylesheet' type='text/css'>
       </head>
       <body>
<?php
require 'config.php' ;
require 'func.php';
$studentID = "";
$studentInfo = getStudentInfo($studentID);
$getStudent = getStudent();
////////////////////////////////////////////////////////////////////////////////////////
//																	REGISTER NEW STUDENT
////////////////////////////////////////////////////////////////////////////////////////
// define variables and set to empty values
$firstNameErr = $lastNameErr = $usernameErr = $emailErr = $passwordErr = $loginErr = "";
$firstName = $lastName = $username = $email = $password = "";
		
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

//first name
	if (empty($_POST["firstName"]))
	{$firstNameErr = "First name is required";}
	else
	{
		$firstName = test_input($_POST["firstName"]);
		$_SESSION['firstName'] = $firstName;
		if (!preg_match("/^[a-zA-Z ]*$/",$firstName))
		{
			$firstNameErr = "Only letters please";
		}
	}
//last name
    if (empty($_POST["lastName"]))
    {
    $lastNameErr = "Last name is required";}
    else
    {
        $lastName = test_input($_POST["lastName"]);
        if (!preg_match("/^[a-zA-Z ]*$/",$lastName))
        {
            $lastNameErr = "Only letters please";
        }
    }
//email	
    if (empty($_POST["email"]))
    {
    $emailErr = "Email is required"; }
    else
    {
        $email = ($_POST["email"]);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $emailErr = "Please enter a valid email address";
        }
	}
//password
	if (empty($_POST["password"]))
	{
	$passwordErr = "Password is required";}
	else
	{
	$password = test_input($_POST["password"]);
	}
		
// username	
	if (empty($_POST["username"]))
	{
	$usernameErr = "Username is required";
	}
	  else
	{
	$username = test_input($_POST["username"]);
	$_SESSION['username'] = $username;
	 if (!preg_match("/^[a-zA-Z0-9]*$/",$username))
		{
			$usernameErr = "Only numbers and letters please";
		}
		}
			
}		
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}


if (empty($firstNameErr) && empty($lastNameErr) && empty($usernameErr) && empty($emailErr) && empty($passwordErr) && (isset($_POST['studentSubmit']))) {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
	$query = "INSERT INTO student (firstName, lastName, username, email, password) VALUES ("
		. "'" . $firstName . "', "
		. "'" . $lastName . "', "
		. "'" . $username . "', "
		. "'" . $email . "', "
		. "'" . $password . "'"
		. ")"	;
$result = mysqli_query($con, $query);
header('LOCATION: selectTest.php?$_POST["username"]');
exit();
}
////////////////////////////////////////////////////////////////////////////////////////
//																 LOG IN EXISTING STUDENT
////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['loginSubmit'])) {
	
	$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
	$query = "SELECT * FROM student WHERE username = "
	. "'" .  ($_POST["username"]) . "'" 
	.  " AND password =" 
	. "'" . ($_POST["password"]) . "'" 
	. "";
	$result = mysqli_query($con, $query);
				
			//check for rows
	$numberOfRows = mysqli_num_rows($result);
	if ($numberOfRows > 0) {
			
header('LOCATION: selectTest.php');
exit();
}

else {
 $loginErr = "Invalid username or password.
Please try again";
	}
}
?>
<header>Learner Centred Design Formative Assessment</header>
<nav>
<?php include 'nav.php' ?>
</nav>
<div id="container" class="one-edge-shadow">
<div id="mainContent">
<!--    <img src="img/Online-learning.jpg" height="120px" class="left"/>
-->  <h1 id="pageTitle">Welcome to the Learner Centred Design Assessment</h1>
<div> <p>In this assessment you will have the opportunity to demonstrate your understanding of what you have learned so far.</p>
    <p>Before you undertake the assessment you should create an account so that you may retrieve your results at your convenience, even when you do not have an internet connection. To get started, please register below:</p></div>
  <div class="half left">  
  <form id="newStudentForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="half">
      <fieldset>
        <p align="right">* Required field</p>
        <legend>First time here?</legend>
        <label for="firstName" class="formLabel">First Name*</label>
        <span class="error">
        <?php echo $firstNameErr;?>
        </span>
        <input type="text" name="firstName" id="firstName" value="<?php echo $firstName;?>"/>
        <label for="lastName" class="formLabel">Last Name*</label>
        <span class="error">
        <?php echo $lastNameErr;?>
        </span>
        <input type="text" name="lastName" id="lastName" value="<?php echo $lastName;?>"/>
        <label for="username" class="formLabel">Username*</label>
        <span class="error">
        <?php echo $usernameErr;?>
        </span><span id="message"></span>
        <input type="text" name="username" id="username" value="<?php echo $username;?>" />
        <label for="email" class="formLabel">Email*</label>
        <span class="error">
        <?php echo $emailErr;?>
        </span>
        <input type="text" name="email" id="email" value="<?php echo $email;?>"/>
        <label for="password" class="formLabel">Password*</label>
        <span class="error">
        <?php echo $passwordErr;?>
        </span>
        <input type="password" name="password" id="password" value="<?php echo $password;?>" maxlength="8" />
        <input type="submit" id="studentSubmit" name="studentSubmit" value="Submit" class="btn" />
      </fieldset>
    </form></div>
  <div class="half right">  <form id="studentLogin" method="post" class="breathe half">
      <fieldset>
        <legend>Returning user?</legend>
        <label for="username" class="formLabel">Username</label>
        <input type="text" name="username" id="username" value="" />
        <label for="password" class="formLabel">Password</label>
        <input type="password" name="password" id="password" value="" maxlength="8" />
        <span class="error">
        <?php echo $loginErr;?>
        </span>
        <input type="submit" id="loginSubmit" name="loginSubmit" value="Log in" class="btn" />
      </fieldset>
    </form></div>
  </div>
</div>
</body>
</html>